import { HttpClient } from "@/axios/HttpClient";
import APIConst from "@/common/constants/APIConst";
import { APIResponse } from "@/common/model/APIResponse";

const MMService = {
  async GetMM(
    table: string,
    orderBy: string,
    IsAccending: boolean,
    searchCriteria = ""
  ) {
    const resObj = {
      tableNeedQuery: table,
      isOuterJoin: false,
      searchCriteria: searchCriteria,
      orderBy: orderBy + "," + (IsAccending ? "ASC" : "DSC"),
    };
    const Response = await HttpClient.post<APIResponse>(
      APIConst.qgl.listvalue,
      resObj
    );
    const data = Response.data;
    if (data.IsSuccess) {
      return Promise.resolve(data.Data);
    } else {
      return Promise.reject([]);
    }
  },

  async SaveMM(table: string, paramsSave: any) {
    const resObj = {
      tableNeedQuery: table,
      data: [JSON.stringify(paramsSave)],
    };
    const Response = await HttpClient.post<APIResponse>(
      APIConst.mm.save,
      resObj
    );
    const data = Response.data;

    if (data.IsSuccess) {
      return Promise.resolve(data.Data);
    } else {
      return Promise.reject(false);
    }
  },

  async DeleteMM(table: string, paramsDelete: any) {
    const resObj = {
      tableNeedQuery: table,
      data: [JSON.stringify(paramsDelete)],
    };
    const Response = await HttpClient.post<APIResponse>(
      APIConst.mm.delete,
      resObj
    );
    const data = Response.data;
    if (data.IsSuccess) {
      return Promise.resolve(data.Data);
    } else {
      return Promise.reject([]);
    }
  },
};
export default MMService;
